/*     */ package com.hp.bon.sgw.core.channel;
/*     */ 
/*     */ import cn.chinatelecom.sgw.soap.SGWService;
/*     */ import cn.chinatelecom.sgw.soap.SgwAck;
/*     */ import cn.chinatelecom.sgw.soap.SgwReq;
/*     */ import com.hp.bon.sgw.bean.ProtocalConvertResult;
/*     */ import com.hp.bon.sgw.bean.XmlMessage;
/*     */ import com.hp.bon.sgw.convert.DataExchangerImpl;
/*     */ import com.hp.bon.sgw.core.BuzProcessorManager;
/*     */ import com.hp.bon.sgw.core.CallParam;
/*     */ import com.hp.bon.sgw.core.channel.handler.IAsynCallHandler;
/*     */ import com.hp.bon.sgw.core.channel.handler.NAVCallHandler;
/*     */ import com.hp.bon.sgw.domain.Node;
/*     */ import com.hp.bon.sgw.domain.SGWChannel;
/*     */ import com.hp.bon.sgw.service.ConfigService;
/*     */ import com.hp.bon.sgw.util.Comutils;
/*     */ import com.hp.bon.sgw.util.LogTool;
/*     */ import com.hp.bon.sgw.util.dcc.DccAvpHelper;
/*     */ import com.hp.ocs.diameter.message.AVP;
/*     */ import com.hp.ocs.diameter.message.Message;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.net.InetAddress;
/*     */ import java.net.InetSocketAddress;
/*     */ import java.util.List;
/*     */ import java.util.concurrent.ConcurrentHashMap;
/*     */ import javax.xml.stream.XMLInputFactory;
/*     */ import javax.xml.stream.XMLStreamReader;
/*     */ import org.apache.commons.lang.StringEscapeUtils;
/*     */ import org.dom4j.DocumentHelper;
/*     */ import org.dom4j.Element;
/*     */ import org.simpleframework.http.ContentType;
/*     */ import org.simpleframework.http.Path;
/*     */ import org.simpleframework.http.Request;
/*     */ import org.simpleframework.http.Response;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ 
/*     */ public class NAVSoapChannel extends SoapChannel
/*     */ {
/*  50 */   private static final Logger logger = LoggerFactory.getLogger(NAVSoapChannel.class);
/*     */ 
/*     */   public NAVSoapChannel(SGWChannel channel) {
/*  53 */     super(channel);
/*     */   }
/*     */ 
/*     */   public void handle(Request request, Response response)
/*     */   {
/*  58 */     logger.info("NAV Soap Channel receive message ...");
/*  59 */     long receiveTime = System.currentTimeMillis();
/*  60 */     PrintStream body = null;
/*     */     try {
/*  62 */       body = response.getPrintStream();
/*  63 */       response.set("Content-Type", "text/xml;charset=utf-8");
/*  64 */       response.set("Server", "HP Asyn WS WebServer )");
/*  65 */       response.setDate("Date", receiveTime);
/*  66 */       response.setDate("Last-Modified", receiveTime);
/*  67 */       ContentType cntType = request.getContentType();
/*  68 */       if (cntType == null) {
/*  69 */         body.println("invalid request Content type is null");
/*  70 */         body.close();
/*  71 */         return;
/*     */       }
/*  73 */       cntType.setCharset(Comutils.fixContentCharSet(cntType.getCharset()));
/*  74 */       String reqContent = request.getContent();
/*  75 */       if (logger.isInfoEnabled()) {
/*  76 */         logger.info("http request from " + request.getClientAddress() + ", " + request.getPath().getPath() + "\r\n" + reqContent);
/*     */       }
/*     */ 
/*  79 */       XmlMessage req = null;
/*     */       try {
/*  81 */         req = parseOldWSRequest(request.getContent());
/*  82 */         logger.info(req.getDATA());
/*     */       }
/*     */       catch (Exception e) {
/*  85 */         XmlMessage ack = createERRAck("", 401, e.toString());
/*  86 */         responseACK(body, ack);
/*  87 */         return;
/*     */       }
/*     */ 
/*  90 */       String serviceCtxID = req.getSCID();
/*  91 */       if (!serviceCtxID.endsWith(this.confSrv.getRealmWithoutAreaCode())) {
/*  92 */         serviceCtxID = DccAvpHelper.replaceSrvCtxID(serviceCtxID, this.confSrv.getRealmWithoutAreaCode());
/*  93 */         req.setSCID(serviceCtxID);
/*     */       }
/*     */ 
/*  96 */       NAVCallHandler asynHandler = new NAVCallHandler(body);
/*  97 */       req.setIp(request.getClientAddress() != null ? request.getClientAddress().getAddress().getHostAddress() : "");
/*  98 */       req.setMsgTime(receiveTime);
/*     */ 
/* 100 */       CallParam callParam = new CallParam(req.getMID(), req.getSCID(), req, req.getUID(), asynHandler);
/*     */ 
/* 102 */       Node node = (Node)ConfigService.name2NodeMap.get(req.getUID());
/* 103 */       if (node == null) {
/* 104 */         callParam.setRetCode(405);
/* 105 */         callParam.setResultMsg("cannot find " + req.getUID());
/* 106 */         asynHandler.onCallResult(callParam);
/* 107 */         return;
/*     */       }
/* 109 */       callParam.setFromNode(node);
/*     */ 
/* 111 */       if (!req.getPWD().equals(node.getPassword())) {
/* 112 */         callParam.setRetCode(406);
/* 113 */         callParam.setResultMsg("password is not match");
/* 114 */         asynHandler.onCallResult(callParam);
/* 115 */         return;
/*     */       }
/*     */ 
/* 118 */       BuzProcessorManager.getInstance().processRequest(callParam);
/*     */     }
/*     */     catch (Exception e) {
/* 121 */       logger.warn("http request err:", e);
/* 122 */       if (body != null) {
/* 123 */         body.println("Server error :" + e.toString());
/* 124 */         body.close();
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public static void responseACK(PrintStream body, XmlMessage ack) throws IOException, UnsupportedEncodingException {
/* 130 */     String respXML = convertSgwAckString(ack);
/* 131 */     byte[] resp = respXML.getBytes("UTF-8");
/* 132 */     ack.setLength(resp.length);
/* 133 */     ack.setMsgTime(System.currentTimeMillis());
/*     */ 
/* 135 */     body.write(respXML.getBytes("UTF-8"));
/* 136 */     body.close();
/* 137 */     if (logger.isInfoEnabled())
/* 138 */       logger.info("http response " + respXML);
/*     */   }
/*     */ 
/*     */   private static XmlMessage createERRAck(String msgID, int retCode, String errMsg)
/*     */   {
/* 143 */     XmlMessage ack = new XmlMessage(false);
/* 144 */     ack.setMID(msgID);
/* 145 */     ack.setRC(Integer.toString(retCode));
/* 146 */     ack.setDS(errMsg);
/* 147 */     return ack;
/*     */   }
/*     */ 
/*     */   private static String convertSgwAckString(XmlMessage ack) {
/* 151 */     String errMsg = ack.getDS();
/* 152 */     errMsg = (errMsg != null) && (errMsg.indexOf('&') == -1) && (errMsg.indexOf('>') == -1) && (errMsg.indexOf('<') == -1) ? errMsg : StringEscapeUtils.escapeXml(errMsg);
/* 153 */     StringBuilder sb = new StringBuilder();
/* 154 */     sb.append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><ns2:SgwAck xmlns:ns2=\"http://www.chinatelecom.cn/SGW/SOAP\">");
/* 155 */     sb.append("<MessageID>").append(ack.getMID()).append("</MessageID>");
/* 156 */     sb.append("<CommandCode>SgwAck</CommandCode>");
/* 157 */     sb.append("<ResponseCode>").append(ack.getRC()).append("</ResponseCode>");
/* 158 */     sb.append("<ErrorMessage>").append(errMsg).append("</ErrorMessage>");
/* 159 */     sb.append("</ns2:SgwAck></soap:Body></soap:Envelope>");
/* 160 */     return sb.toString();
/*     */   }
/*     */ 
/*     */   private static XmlMessage parseOldWSRequest(String representation) throws Exception {
/* 164 */     InputStream input = new ByteArrayInputStream(representation.getBytes("UTF-8"));
/* 165 */     XMLStreamReader xmlStreamReader = inputFactory.createXMLStreamReader(input);
/* 166 */     XmlMessage req = new XmlMessage(true);
/* 167 */     req.setLength(representation.getBytes().length);
/* 168 */     int nextType = 0;
/*     */ 
/* 170 */     boolean seenSgwQueryReq = false;
/* 171 */     while (xmlStreamReader.hasNext()) {
/* 172 */       nextType = xmlStreamReader.next();
/* 173 */       if (nextType == 1) {
/* 174 */         String elName = xmlStreamReader.getLocalName();
/* 175 */         if (elName.equals("SgwReq")) {
/* 176 */           seenSgwQueryReq = true;
/* 177 */         } else if (seenSgwQueryReq) {
/* 178 */           String value = xmlStreamReader.getElementText();
/* 179 */           if (elName.equals("MessageID"))
/* 180 */             req.setMID(value);
/* 181 */           else if (elName.equals("ServiceContextID"))
/* 182 */             req.setSCID(value);
/* 183 */           else if (elName.equals("UserName"))
/* 184 */             req.setUID(value);
/* 185 */           else if (elName.equals("PassWord"))
/* 186 */             req.setPWD(value);
/* 187 */           else if (elName.equals("RetransmitMmessage"))
/* 188 */             req.setREPTAG(Integer.valueOf(value).intValue());
/* 189 */           else if (elName.equals("DataInfomation")) {
/* 190 */             if (value.contains("<Service_Information>"))
/* 191 */               req.setDATA(value);
/*     */             else {
/* 193 */               req.setDATA("<Service_Information><Data_Information><Data_Table_Simple_Row><![CDATA[" + value + "]]></Data_Table_Simple_Row></Data_Information></Service_Information>");
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 200 */     return req;
/*     */   }
/*     */ 
/*     */   public int sendRequest(CallParam callParam)
/*     */   {
/* 206 */     SGWService sgwServiceClient = Comutils.getSGWServiceClient(callParam.getToNode().getServerUrl());
/*     */ 
/* 208 */     SgwReq sgwreq = new SgwReq();
/* 209 */     sgwreq.setMessageID(callParam.getSessionId());
/* 210 */     sgwreq.setUserName(callParam.getUserName());
/* 211 */     sgwreq.setRetransmitMmessage("0");
/*     */ 
/* 213 */     if ((callParam.getRequest() instanceof XmlMessage)) {
/* 214 */       sgwreq.getDataInfomation().add(((XmlMessage)callParam.getRequest()).getDATA());
/* 215 */     } else if ((callParam.getRequest() instanceof Message)) {
/* 216 */       AVP sgwRootAVP = DccAvpHelper.createSGWRootAVPFromMessage((Message)callParam.getRequest());
/* 217 */       ProtocalConvertResult result = DataExchangerImpl.getInstance().convertFromCCRRequest(callParam.getServiceContextId(), sgwRootAVP);
/* 218 */       if (result.getRetCode() == 0)
/*     */       {
/* 220 */         Element srvInfEl = ((Element)result.getResult()).element("Service_Information");
/*     */ 
/* 222 */         sgwreq.getDataInfomation().add(srvInfEl.asXML());
/*     */       }
/*     */       else {
/* 225 */         callParam.setRetCode(result.getRetCode());
/* 226 */         callParam.setResultMsg(result.getErrMsg());
/* 227 */         callParam.getAsynHandler().onCallResult(callParam);
/* 228 */         return -1;
/*     */       }
/*     */     }
/*     */ 
/* 232 */     SgwAck sgwAck = null;
/*     */     try {
/* 234 */       sgwAck = sgwServiceClient.sgwRequest(sgwreq);
/*     */ 
/* 236 */       XmlMessage ack = new XmlMessage(false);
/* 237 */       ack.setMsgTime(System.currentTimeMillis());
/* 238 */       ack.setMID(sgwAck.getMessageID());
/* 239 */       ack.setSIG(null);
/* 240 */       ack.setRC(sgwAck.getResponseCode());
/* 241 */       ack.setDS(sgwAck.getErrorMessage());
/*     */ 
/* 243 */       Element serviceInformation = DocumentHelper.createElement("Service_Information");
/*     */ 
/* 245 */       Element rc = serviceInformation.addElement("Service_Result_Code");
/* 246 */       rc.setText(sgwAck.getResponseCode());
/*     */ 
/* 248 */       if ((sgwAck.getErrorMessage() != null) && (!"".equals(sgwAck.getErrorMessage())))
/*     */       {
/* 250 */         rc = serviceInformation.addElement("Para_Field_Result");
/* 251 */         rc.setText(sgwAck.getErrorMessage());
/*     */       }
/*     */ 
/* 254 */       ack.setDataElement(serviceInformation);
/*     */ 
/* 256 */       callParam.setRecvResponse(ack);
/*     */ 
/* 258 */       int callRc = Integer.parseInt(sgwAck.getResponseCode());
/* 259 */       callParam.setRetCode(callRc);
/* 260 */       callParam.setResultMsg(sgwAck.getErrorMessage());
/*     */     }
/*     */     catch (Exception e) {
/* 263 */       logger.warn("call ws service err:", e);
/* 264 */       int retCode = LogTool.getErrorCodeForBonCallException(e);
/* 265 */       callParam.setRetCode(retCode);
/* 266 */       callParam.setResultMsg("Call ws service err:" + e.getLocalizedMessage());
/*     */     }
/* 268 */     callParam.getAsynHandler().onCallResult(callParam);
/*     */ 
/* 270 */     return 0;
/*     */   }
/*     */ }

/* Location:           C:\Users\think\Desktop\sgw_2.1.jar
 * Qualified Name:     com.hp.bon.sgw.core.channel.NAVSoapChannel
 * JD-Core Version:    0.6.1
 */